// UEUtil.cpp: implementation of the UEUtil class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "UEUtil.h"
#include "UEPipeComponent.h"

AcGeTol UEUtil::m_tol;

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

UEUtil::UEUtil()
{
	UEUtil::m_tol.setEqualPoint ( 1.0e-5 );
	UEUtil::m_tol.setEqualVector ( 1.0e-6 );
}

UEUtil::~UEUtil()
{

}

ads_name* UEUtil::NewAdsName()
{
	return reinterpret_cast<long(*)[2]>( new ads_name );
}

void UEUtil::AdsNamePtrArrayAdd ( CAdsNamePtrArray& arn, ads_name* pen )
{
	arn.Add ( pen );
}

void UEUtil::AdsNamePtrArrayRemoveAt ( CAdsNamePtrArray& arn, const int iIndex )
{
	if ( iIndex >= 0 && iIndex < arn.GetSize() )
	{
		delete [] arn.GetAt ( iIndex );
		arn.RemoveAt ( iIndex );
	}
}

void UEUtil::AdsNamePtrArrayRemoveAll ( CAdsNamePtrArray& arn )
{
	for ( int i = 0; i < arn.GetSize(); i++ )
	{
		delete [] arn.GetAt ( i );
	}
	arn.RemoveAll();
}

template<class T>
BOOL UEUtil::IsA ( ads_name& en )
{
	AcDbObject* pObj = UEPipeComponent::OpenObjectRead( en );
	if ( pObj->isKindOf( T::desc() ) == Adesk::kTrue )
	{
		pObj->close();
		return TRUE;
	}
	else
	{
		pObj->close();
		return FALSE;
	}
}

BOOL UEUtil::GetVectorOfLine ( AcGeVector3d& vFire, ads_name en )
{
	AcDbEntity* pEnt = NULL;
	pEnt = UEPipeComponent::OpenEntityRead( en );
	if ( pEnt == NULL )
		return FALSE;

	AcDbLine* pLn = NULL;
	pLn = AcDbLine::cast( pEnt );
	if ( pLn == NULL )
	{
		pEnt->close();
		return FALSE;
	}

	vFire = pLn->endPoint() - pLn->startPoint();
	pEnt->close();
	
	return TRUE;
}
